package com.example.loganalysis.controller;

import com.example.loganalysis.dao.TOperateLogDao;
import com.example.loganalysis.entity.TOperateLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/logs")
public class OperateLogController {

    private static final Logger logger = LoggerFactory.getLogger(OperateLogController.class);

    private final TOperateLogDao tOperateLogDao;

    @Autowired
    public OperateLogController(TOperateLogDao tOperateLogDao) {
        this.tOperateLogDao = tOperateLogDao;
    }

    @PostMapping("/insertoperate")
    public String insertLog(@RequestBody TOperateLog tOperateLog) {
        logger.info("Received operatelog: {}", tOperateLog);
        try {
            // 插入数据库
            tOperateLogDao.insertOperateLogs(tOperateLog);
            logger.info("operateLog inserted successfully: {}", tOperateLog);
            return "operteLog inserted successfully.";
        } catch (Exception e) {
            logger.error("Error inserting operatelog: {}", tOperateLog, e);
            return "Error inserting operatelog.";
        }
    }

    @GetMapping("/operate_ipfs_hash")
    public ResponseEntity<String> getOperateIpfsHash(@RequestParam String log_id) {
        String ipfsHash = tOperateLogDao.getIpfsHashByLogId(log_id);
        if (ipfsHash == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Operate IPFS Hash not found for log_id: " + log_id);
        }
        return ResponseEntity.ok(ipfsHash);
    }
}
